package com.review.sort;

import java.util.ArrayList;

public class QuickSort {
    //快速排序{1,2,3,4,5,6,7,9,10}
    public void quickSort(int[] array, int start, int end) {
        if (start >= end) {//退出条件
            return;
        }
        int key = array[start];//以第一个数为标准
        int i = start;
        int j = end;
        while (i < j) {
            while (i < j && array[j] > key) {
                j--;
            }
            while (i < j && array[i] < key) {
                i++;
            }
            if (i < j && array[i] == array[j]) {
                i++;
            } else {
                int tmp = array[i];
                array[i] = array[j];
                array[j] = tmp;
            }
        }
        //此时选出数的左边都比此数小，右边都比此数大
        quickSort(array, start, i - 1);//左边数组
        quickSort(array, j + 1, end);//右边数组
    }

    public static void main(String[] args) {
        int[] ints = {8, 6, 7, 5, 9, 4, 1, 10, 2, 3};
        new QuickSort().quickSort(ints, 0, ints.length - 1);
        for (int anInt : ints) {
            System.out.print(anInt + ",");
        }
    }
}
